﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Security.Cryptography;
using System.Text;
using BanaToys.InfoSys.Bll;
using BanaToys.InfoSys.Bol;
using Newtonsoft.Json;


namespace BanaToys.InfoSys.Web.ApiCall.BanaWww
{
    /// <summary>
    /// API调用者
    /// </summary>
    public class Tag_ApiCall
    {
        private const string Url_Sync = "v30/tag/tag.ashx?action=sync";
        private const string Url_Delete = "v30/tag/tag.ashx?action=delete";
        private const string Url_List = "v30/tag/tag.ashx?action=list";
        private const string Url_Detail = "v30/tag/tag.ashx?action=detail";
        private const string Url_State = "v30/tag/tag.ashx?action=state";

        #region 1.同步,若是新增则默认上架

        /// <summary>
        /// 同步并上架显示
        /// </summary>
        /// <param name="apiTag"></param>
        /// <param name="message"></param>
        /// <returns></returns>
        public static bool Sync(i_tag_item apiTag, out string message)
        {
            //
            message = null;

            //
            i_tag_item item = apiTag;
            q_tag_sync sync = new q_tag_sync();
            sync.Item = item;

            string para_json = JsonConvert.SerializeObject(sync);

            //调用接口
            string url_full = API_Config.API_Url.TrimEnd('/') + "/" + Url_Sync.TrimStart('/');

            //header
            Dictionary<string, string> dictHeader = API_Config.GetHeaderAuth();

            //
            string json;
            string errorText;
            bool ok = HttpHelp.PostFunction(url_full, para_json, dictHeader, "POST", out json, out errorText);
            if (!ok)
            {
                message = "错误:" + errorText;
                return false;
            }

            //ZBug.Log(json);

            //解析JSON
            a_tag_sync sync_msg = null;
            try
            {
                sync_msg = JsonConvert.DeserializeObject<a_tag_sync>(json);
            }
            catch (Exception ex)
            {
                message = "解析服务端返回JSON失败:" + ex.Message;
                return false;
            }

            if (sync_msg == null)
            {
                message = "服务端返回空";
                return false;
            }

            //业务判断
            message = string.Format("{0}", sync_msg.errmsg);
            if (sync_msg.errcode != 0)
            {
                message = string.Format("api返回失败:{0}({1})", sync_msg.errmsg, sync_msg.errcode);
                return false;
            }

            //返回
            return true;
        }

        /// <summary>
        /// 同步系列品类转换成远程json
        /// </summary>
        /// <param name="localTag"></param>
        /// <returns></returns>
        public static i_tag_item convert_tag_to_i(BnGoodsTag localTag)
        {
            if (localTag == null)
            {
                return null;
            }

            i_tag_item item = new i_tag_item();
            item.FRowGuid = localTag.FRowGuid.ToString().Trim('{', '}').ToLower();
            item.FParentGuid = localTag.FParentGuid.HasValue ? localTag.FParentGuid.Value.ToString().Trim('{','}') : string.Empty;
            item.FTagNameChs = localTag.FTagNameChs;
            item.FTagNameEng = localTag.FTagNameEng;
            item.FDetail = localTag.FDetail;
            item.FTreeCode = localTag.FTreeCode;
            item.FTreeCodePath = localTag.FTreeCodePath;
            //item.FOrderNo = localTag.FOrderNo;
            item.FRemark = localTag.FRemark;
            //item.FIsDisabled = localTag.FIsDisabled;

            //
            return item;
        }

        /// <summary>
        /// 
        /// </summary>
        private void Test_Sync()
        {
            //string barcode = "901010001";
            //BnProduct bp = new BnProductBll().SysGet(barcode);
            //if (bp == null)
            //{
            //    Console.WriteLine("没有这个条码:" + barcode + ".");
            //    return;
            //}
            //Console.WriteLine(bp.ToMemberString());

            ////
            //i_tag_item item = convert_tag_to_i(bp);
            //if (item == null)
            //{
            //    Console.WriteLine("转换失败");
            //    return;
            //}
            //string message;
            //bool call_ok = Sync(item, out message);
            //Console.WriteLine("是否成功={0},返回={1}", call_ok, message);
        }

        #endregion

        #region 2.下架或上架

        /// <summary>
        /// 
        /// </summary>
        /// <param name="q"></param>
        /// <param name="a"></param>
        /// <param name="message"></param>
        /// <returns></returns>
        public static bool UpdateState(q_tag_state q, out a_tag_state a, out string message)
        {
            a = null;
            message = null;

            //
            if (q == null)
            {
                message = "请求参数对象为空";
                return false;
            }

            //json ------------------------------------------------------------
            string para_json = JsonConvert.SerializeObject(q);

            //调用接口
            string url_full = API_Config.API_Url.TrimEnd('/') + "/" + Url_State.TrimStart('/');

            //header
            Dictionary<string, string> dictHeader = API_Config.GetHeaderAuth();

            //
            string json;
            string errorText;
            bool ok = HttpHelp.PostFunction(url_full, para_json, dictHeader, "POST", out json, out errorText);
            if (!ok)
            {
                message = "错误:" + errorText;
                return false;
            }

            //ZBug.Log(json);
            //Debug.WriteLine(json);

            //解析Json
            //a_tag_state a = null;
            try
            {
                a = JsonConvert.DeserializeObject<a_tag_state>(json);
            }
            catch (Exception ex)
            {
                message = "解析服务端返回JSON失败:" + ex.Message;
                return false;
            }
            if (a == null)
            {
                message = "服务端返回空";
                return false;
            }

            //解析业务
            if (a.errcode != 0)
            {
                message = string.Format("服务端返回失败:{0}({1})", a.errmsg, a.errcode);
                return false;
            }

            //return
            //detail_ok = detail;
            return true;
        }

        private void Test_UpdateState()
        {
            ////请求参数,全部忽略
            //q_tag_state q = new q_tag_state();
            //q.FBarcode = "901010001";
            //q.FIsDisabled = false; //正常显示
            ////q.FIsDisabled = true; //禁用

            ////接收响应数据
            //a_tag_state a;

            //string message;
            //bool call_ok = UpdateState(q, out a, out message);
            //Debug.WriteLine(string.Format("是否成功={0},返回={1}", call_ok, message));
            //if (call_ok && a != null)
            //{
            //    Debug.WriteLine(Help.ObjectToString(a));
            //}
        }

        #endregion

        #region 3.直接删除

        /// <summary>
        /// 删除
        /// </summary>
        /// <param name="q"></param>
        /// <param name="a"></param>
        /// <param name="message"></param>
        /// <returns></returns>
        public static bool Delete(q_tag_delete q,out a_tag_delete a,out string message)
        {
            a = null;
            message = null;

            //
            if (q == null)
            {
                message = "请求参数对象为空";
                return false;
            }

            //json ------------------------------------------------------------
            string para_json = JsonConvert.SerializeObject(q);

            //调用接口
            string url_full = API_Config.API_Url.TrimEnd('/') + "/" + Url_Delete.TrimStart('/');

            //header
            Dictionary<string, string> dictHeader = API_Config.GetHeaderAuth();

            //
            string json;
            string errorText;
            bool ok = HttpHelp.PostFunction(url_full, para_json, dictHeader, "POST", out json, out errorText);
            if (!ok)
            {
                message = "错误:" + errorText;
                return false;
            }

            //ZBug.Log(json);
            //Debug.WriteLine(json);

            //解析Json
            //a_tag_delete a = null;
            try
            {
                a = JsonConvert.DeserializeObject<a_tag_delete>(json);
            }
            catch (Exception ex)
            {
                message = "解析服务端返回JSON失败:" + ex.Message;
                return false;
            }
            if (a == null)
            {
                message = "服务端返回空";
                return false;
            }

            //解析业务
            if (a.errcode != 0)
            {
                message = string.Format("服务端返回失败:{0}({1})", a.errmsg, a.errcode);
                return false;
            }

            //return
            //detail_ok = detail;
            return true;
        }

        private void Test_Delete()
        {
            ////请求参数,全部忽略
            //q_tag_delete q = new q_tag_delete();
            //q.FBarcode = "901010001";

            ////接收响应数据
            //a_tag_delete a;

            //string message;
            //bool call_ok = Delete(q, out a, out message);
            //Debug.WriteLine(string.Format("是否成功={0},返回={1}", call_ok, message));
            //if (call_ok && a != null)
            //{
            //    Debug.WriteLine(Help.ObjectToString(a));
            //}
        }

        #endregion

        #region 4.展示列表

        /// <summary>
        /// 
        /// </summary>
        /// <param name="q">请求q参数</param>
        /// <param name="list_ok">输出：响应a数据</param>
        /// <param name="message">输出：错误消息</param>
        /// <returns></returns>
        public static bool GetList(q_tag_list q, out a_tag_list list_ok, out string message)
        {
            list_ok = null;
            message = null;

            //
            //Hashtable htSync = new Hashtable();
            //htSync.Add("PageSize", PageSize);
            //htSync.Add("PageIndex", pageIndex);
            //q_tag_list q = new q_tag_list();
            //q.PageSize = PageSize;
            //q.PageIndex = pageIndex;
            //if (!pageIndex.HasValue) pageIndex = 0;
            if (q == null)
            {
                message = "请求参数对象为空";
                return false;
            }

            //json ------------------------------------------------------------
            string para_json = JsonConvert.SerializeObject(q);

            //调用接口
            string url_full = API_Config.API_Url.TrimEnd('/') + "/" + Url_List.TrimStart('/');

            //header
            Dictionary<string, string> dictHeader = API_Config.GetHeaderAuth();

            //
            string json;
            string errorText;
            bool ok = HttpHelp.PostFunction(url_full, para_json, dictHeader,"POST", out json, out errorText);
            if (!ok)
            {
                message = "错误:" + errorText;
                return false;
            }

            //ZBug.Log(json);
            //Debug.WriteLine(json);

            //解析Json
            a_tag_list list = null;
            try
            {
                list = JsonConvert.DeserializeObject<a_tag_list>(json);
            }
            catch (Exception ex)
            {
                message = "解析服务端返回JSON失败:" + ex.Message;
                return false;
            }
            if (list == null)
            {
                message = "服务端返回空";
                return false;
            }

            //解析业务
            if (list.errcode != 0)
            {
                message = string.Format("服务端返回失败:{0}({1})", list.errmsg, list.errcode);
                return false;
            }

            //return
            list_ok = list;
            return true;
        }

        private void Test_GetList()
        {
            ////请求参数,全部忽略
            //q_tag_list q = new q_tag_list();
            //q.Barcode = "901010001";

            ////接收响应数据
            //a_tag_list a;

            //string message;
            //bool call_ok = GetList(q, out a, out message);
            //Debug.WriteLine(string.Format("是否成功={0},返回={1}", call_ok, message));
            //if (call_ok && a != null)
            //{
            //    Debug.WriteLine(Help.ObjectToString(a));
            //    if (a.List != null)
            //    {
            //        Debug.WriteLine("有"+a.List.Count+"个");
            //    }
            //}
        }

        #endregion

        #region 5.展示详情

        /// <summary>
        /// 
        /// </summary>
        /// <param name="q">请求q参数</param>
        /// <param name="detail_ok">输出：响应a数据</param>
        /// <param name="message">输出：错误消息</param>
        /// <returns></returns>
        public static bool GetDetail(q_tag_detail q, out a_tag_detail detail_ok, out string message)
        {
            detail_ok = null;
            message = null;

            //
            if (q == null)
            {
                message = "请求参数对象为空";
                return false;
            }

            //json ------------------------------------------------------------
            string para_json = JsonConvert.SerializeObject(q);

            //调用接口
            string url_full = API_Config.API_Url.TrimEnd('/') + "/" + Url_Detail.TrimStart('/');

            //header
            Dictionary<string, string> dictHeader = API_Config.GetHeaderAuth();

            //
            string json;
            string errorText;
            bool ok = HttpHelp.PostFunction(url_full, para_json, dictHeader, "POST", out json, out errorText);
            if (!ok)
            {
                message = "错误:" + errorText;
                return false;
            }

            //ZBug.Log(json);
            //Debug.WriteLine(json);

            //解析Json
            a_tag_detail detail = null;
            try
            {
                detail = JsonConvert.DeserializeObject<a_tag_detail>(json);
            }
            catch (Exception ex)
            {
                message = "解析服务端返回JSON失败:" + ex.Message;
                return false;
            }
            if (detail == null)
            {
                message = "服务端返回空";
                return false;
            }

            //解析业务
            if (detail.errcode != 0)
            {
                message = string.Format("服务端返回失败:{0}({1})", detail.errmsg, detail.errcode);
                return false;
            }

            //return
            detail_ok = detail;
            return true;
        }

        private void Test_GetDetail()
        {
            ////请求参数,全部忽略
            //q_tag_detail q = new q_tag_detail();
            //q.FBarcode = "901010001";

            ////接收响应数据
            //a_tag_detail a;

            //string message;
            //bool call_ok = GetDetail(q, out a, out message);
            //Debug.WriteLine(string.Format("是否成功={0},返回={1}", call_ok, message));
            //if (call_ok && a != null)
            //{
            //    Debug.WriteLine(Help.ObjectToString(a));
            //    if (a.Item != null)
            //    {
            //        Debug.WriteLine(Help.ObjectToString(a.Item));
            //    }
            //}
        }

        #endregion

    }
}